package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysposition.dto.SysPositionDTO;
import com.talkweb.newproject.admin.sysposition.service.SysPositionService;
import com.talkweb.newproject.api.model.admin.SysPositionParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.SqlResultSetMapping;

/**
 * Created by qsf on 2019/5/8.
 */
@RestController
@RequestMapping(value="/api/sysPosition")
@Slf4j
public class SysPositionRest {
    @Autowired
    private SysPositionService sysPositionService;

    /**
     * 根据id查询position信息
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysPositionDTO> querySysPositionByPrimaryKey(@PathVariable("id") String id) throws Exception {
        ResponseResult<SysPositionDTO> result = new ResponseResult<>();
        try {
            SysPositionDTO sysPositionDTO = sysPositionService.querySysPositionByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysPositionDTO);
            result.setMessage("查询成功");
            return result;
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }
    }

    /**
     * 根据条件查询position对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method=RequestMethod.POST)
    public ResponseResult<SysPositionDTO> queryLike(@RequestBody SysPositionDTO obj) throws Exception {
        ResponseResult<SysPositionDTO> result = new ResponseResult<>();
        try{
            SysPositionDTO sysPositionDTO = sysPositionService.queryLikeSysPostion(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(sysPositionDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }
    }

    /**
     * 查询position列表
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/getSysPositionList",method = RequestMethod.POST)
    public Object querySysPositionList(@RequestBody SysPositionParam obj) throws Exception {
        return sysPositionService.searchSysPosition(obj);
    }

    /**
     * 新增position信息
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    @ApiOperation(value="新增对象数据",notes="返回结果,成功:200,失败:500",httpMethod = "POST")
    public ResponseResult<SysPositionDTO>  create(@RequestBody SysPositionDTO obj) throws Exception {
        ResponseResult<SysPositionDTO> result=new ResponseResult<>();
        try{
            sysPositionService.inserSysPosition(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e ) {
            log.info(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/update/v1",method=RequestMethod.POST)
    public ResponseResult update(@RequestBody SysPositionDTO obj) throws Exception {
        ResponseResult result = new ResponseResult<>();
        try{
            sysPositionService.updateSysPosition(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    public ResponseResult deleteSysPositionById(@RequestParam("ids") String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(ids!=null || !"".equals(ids) ) {
                sysPositionService.deleteSysPositionByPrimaryKey(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败,请重试");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
 }
